package edu.aut.bme.hu.orkomet.engine;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.tdb.TDBFactory;

import edu.aut.bme.hu.orkomet.Messages;
import edu.aut.bme.hu.orkomet.dal.DerbyDAL;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RDFEngine {
    private static ModelMaker maker = ModelFactory.createModelRDBMaker(DerbyDAL.getConnection());
    private static final String MODEL_NAME="Orkomet"; //$NON-NLS-1$
    private static final String SCHEMA_FILE="/MediaLibrarySchema.owl"; //$NON-NLS-1$
    private static final String DATA_FILE="/MediaLibrary.owl"; //$NON-NLS-1$
    private static final String TDB_PATH="tdb"; //$NON-NLS-1$
    private static Model orkometModel;

    public static Model GetModel() {
    	if(orkometModel!=null)
    		return orkometModel;
    	else
    	{
    		orkometModel=TDBFactory.createModel(TDB_PATH);
    		Logger.getLogger(RDFEngine.class.getName()).log(Level.INFO,new File(".").getAbsolutePath());
    		return orkometModel;
    	}
    }
    
    
    protected static Model GetModelSDB() {
        orkometModel = maker.createModel(MODEL_NAME);
        if (orkometModel.isEmpty()) {
        	Logger.getLogger(RDFEngine.class.getName()).log(Level.INFO,Messages.RDFEngine_Inferring);
            orkometModel.removeAll();
            InputStream is = null;
            try {
            	Logger.getLogger(RDFEngine.class.getName()).log(Level.INFO,Messages.RDFEngine_InferringSchema);
                is = RDFEngine.class.getResourceAsStream(SCHEMA_FILE);                
                orkometModel.read(is, ""); //$NON-NLS-1$
                is.close();
                Logger.getLogger(RDFEngine.class.getName()).log(Level.INFO,Messages.RDFEngine_InferringData);
                is = RDFEngine.class.getResourceAsStream(DATA_FILE);                
                orkometModel.read(is, ""); //$NON-NLS-1$
                is.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(RDFEngine.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(RDFEngine.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                       if(is!=null)
                           is.close();
                } catch (IOException ex) {
                    Logger.getLogger(RDFEngine.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return orkometModel;
    }
    
    public static void InitLog()
    {
    	Logger.getLogger(RDFEngine.class.getName()).log(Level.INFO,Messages.RDFEngine_InitModel);
        Model m=GetModel();
     /*   String modelsize=String.valueOf( m.size());
        */
        String modelsize="*UNKNOWN* (TDB)";    
        Logger.getLogger(RDFEngine.class.getName()).log(Level.INFO,String.format(Messages.RDFEngine_FMT_Loaded,MODEL_NAME,modelsize) );     
    }
}

